/*    
 * THis file is part of the xoperator SPARQL/XMPP agent.
 * For further information see: http://xoperator.sf.net
 * Copyright (C) 2007  Jörg Unbehauen  

 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 */
package org.aksw.xoperator.sparql.p2p;

import org.aksw.xoperator.Constants;
import org.aksw.xoperator.Controller;
import org.aksw.xoperator.sparql.SparqlQuery;
import org.aksw.xoperator.xmpp.JabberClientManager;
import org.jdom.Element;
import org.jdom.input.DOMBuilder;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.provider.IQProvider;
import org.jivesoftware.smack.util.StringUtils;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.dom2_builder.DOM2XmlPullBuilder;


/**
 * A p2p transportation wrapper for sparql queries;
 * 
 * @author joerg
 * 
 */
public class SPARQLQueryOverXmpp extends IQ {

	private int htl = 1;

	private String query;

	public String getQuery() {
		return this.query;
	}

	public void setQuery(String query) {
		this.query = query;
	}

	

	public SPARQLQueryOverXmpp(String query) {
		super();
		this.query = query;
	}
	
	public SPARQLQueryOverXmpp(SparqlQuery query){
		super();
		this.query = query.getQueryString();
		
	}

	@Override
	public String getChildElementXML() {
		StringBuffer buffer = new StringBuffer();
		buffer.append("<query xmlns=\"");
		buffer.append(Constants.SPARQL_XMPP_SPEC_URL);
		buffer.append("\">");
		buffer.append(StringUtils.escapeForXML(query));
		buffer.append("</query>");

		return buffer.toString();
	}
	
	
	
	

	public static class Provider implements IQProvider {

		public IQ parseIQ(XmlPullParser xpp) throws Exception {
			
			Element ele = new DOMBuilder().build(new DOM2XmlPullBuilder().parseSubTree(xpp));
			

			return new SPARQLQueryOverXmpp(ele.getText());
		}
	}
	
	public static class IQPaketListener implements PacketListener{

		private Controller control;
		private JabberClientManager jcManager;
		
		public IQPaketListener(Controller control, JabberClientManager jcManager) {
			this.control = control;
			this.jcManager = jcManager;
		}

		public void processPacket(Packet arg0) {
			SPARQLQueryOverXmpp query = (SPARQLQueryOverXmpp) arg0;
			if(query.getType().equals(IQ.Type.GET)){
				String result = control.processIncomingSparqlQuery(query);
				
				SPARQLResultoverXmpp xmppres = new SPARQLResultoverXmpp(result);
				xmppres.setTo(query.getFrom());
				xmppres.setPacketID(query.getPacketID());
				jcManager.sendPacket(xmppres);
				
			}
			
			
			
		}
	}
	
	public static class ClassFilter implements PacketFilter{

		public boolean accept(Packet arg0) {
			
			boolean returnValue = false;
			if(arg0 instanceof SPARQLQueryOverXmpp){
				returnValue = true;
			}
			return returnValue;
		}
		
	}
	
	
	
	public int getHtl() {
		return htl;
	}
	
	public void setHtl(int htl) {
		this.htl = htl;
	}
	
	
	public SparqlQuery getSparqlQuery(){
		SparqlQuery squery = new SparqlQuery(this.query);
		return squery;
	}

}
